package com.sun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.common.Result;
import com.sun.common.SystemCrmlog;
import com.sun.entity.Drug;
import com.sun.entity.Problem;
import com.sun.service.DrugService;
import com.sun.service.ProblemService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Project: hospitalDrugInformationManagement
 * @Author: 86178
 * @Date: 2023/4/3 11:53
 * @FileName: ProblemController
 * @Description: ""
 */
@RestController
@RequestMapping("/problem")
public class ProblemController {

    @Autowired
    private ProblemService problemService;

    @Autowired
    private DrugService drugService;

    /**
     * 分页查询问题药品数据
     *
     * @param pageNum
     * @param pageSize
     * @param drugName
     * @return
     */
    @GetMapping("/queryProblemByPage")
    public Result<Page> queryProblemByPage(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                           @RequestParam(value = "limit", defaultValue = "10") int pageSize,
                                           String drugName, String drugCode,
                                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {

        Page<Problem> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Problem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(drugName), Problem::getDrugName, drugName);
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(drugCode), Problem::getDrugCode, drugCode);
        lambdaQueryWrapper.gt(startTime != null, Problem::getCreateTime, startTime);
        lambdaQueryWrapper.lt(endTime != null, Problem::getCreateTime, endTime);
        problemService.page(pageInfo, lambdaQueryWrapper);
        return Result.success(pageInfo);
    }


    /**
     * 添加一个问题药品
     */
    @SystemCrmlog(title = "库存管理模块", type = "INSERT", description = "添加一个问题药品")
    @PostMapping("/addProblem")
    public Result<String> addProblem(@RequestBody Problem problem) {
        if (problem.getDrugId() == null) {
            LambdaQueryWrapper<Drug> lambdaQueryWrapper = new LambdaQueryWrapper<Drug>();
            lambdaQueryWrapper.like(Drug::getDrugName, problem.getDrugName());
            Long drugId = drugService.getOne(lambdaQueryWrapper).getDrugId();
            problem.setDrugId(drugId);
        }
        problem.setCreateTime(LocalDateTime.now());
        problemService.save(problem);
        return Result.success("新增成功");
    }

    /**
     * 删除一个问题药品
     */
    @SystemCrmlog(title = "库存管理模块", type = "DELETE", description = "删除一个问题药品")
    @DeleteMapping("/deleteProblem/{id}")
    public Object deleteProblem(@PathVariable Long id) {
        problemService.removeById(id);
        return Result.success("删除成功");
    }


    /**
     * 修改一个问题药品
     */
    @SystemCrmlog(title = "库存管理模块", type = "UPDATE", description = "修改一个问题药品")
    @PutMapping("/updateProblem")
    public Object updateProblem(@RequestBody Problem problem) {
        problemService.updateById(problem);
        return Result.success("修改成功");
    }
}
